﻿using System;
using System.Linq;
using System.Windows.Controls;

namespace Tools.TextBox
{
    public static class TextBoxUpravy
    {
        public static void smazatText(object sender, TextChangedEventArgs e)
        {
            System.Windows.Controls.TextBox textBox = sender as System.Windows.Controls.TextBox;
            int iValue = -1;

            if (Int32.TryParse(textBox.Text, out iValue) == false)
            {
                TextChange textChange = e.Changes.ElementAt<TextChange>(0);
                int iAddedLength = textChange.AddedLength;
                int iOffset = textChange.Offset;

                textBox.Text = textBox.Text.Remove(iOffset, iAddedLength);
            }
            textBox.Select(textBox.Text.Length, textBox.Text.Length);
        }


        /// <summary>
        /// Upozornuje na případné probémy v poly
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="nameOfComponent"></param>
        /// <returns></returns>
        public static bool Validate(object sender, string nameOfComponent)
        {
            System.Windows.Controls.TextBox textBox = sender as System.Windows.Controls.TextBox;
            if (textBox.Text == "")
            {
                System.Windows.MessageBox.Show("Pozor pole " + nameOfComponent + " nesmí zůstat prázdné","Pozor",System.Windows.MessageBoxButton.OK,System.Windows.MessageBoxImage.Warning);
                return false;
            }
            return true;
        }
    }
}
